package com.luxury.fragment.party_fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.luxury.R;
import com.luxury.adpter.GlobalRecylerAdapter;
import com.luxury.bean.GlobalChildBean;
import com.luxury.decoration.SpacesItemDecoration;
import com.luxury.r_innerface.InterfaceLuxury;
import com.luxury.utils.URLContants;

import java.util.ArrayList;
import java.util.List;


import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

import static com.luxury.adpter.HomeRecyclerAdapter1.TAG;

/**
 * A simple {@link Fragment} subclass.
 */
public class GlobalFragment_Child extends Fragment {

    private RecyclerView RV_Globalchild;
    private Context mContext;
    List<GlobalChildBean.CommentShowDisplaysBean> mList = new ArrayList<>();

    public GlobalFragment_Child() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    public static GlobalFragment_Child newInstance() {
        Bundle args = new Bundle();
        GlobalFragment_Child fragment = new GlobalFragment_Child();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recyclerview, container, false);
        RV_Globalchild = (RecyclerView) view.findViewById(R.id.recycler_sale);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        loadNetData();
    }

    /**
     * 加载网络数据
     */
    private void loadNetData() {
        //利用retrofit请求网络数据
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URLContants.BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
        InterfaceLuxury interfaceLuxury = retrofit.create(InterfaceLuxury.class);
        //获取全球晒货child
        final String ID = getArguments().getString("ID");

            //处理字符串
            Call<String> GlobalChildString = interfaceLuxury.getGlobalChildString(ID);
            GlobalChildString.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    String str = response.body();
                    int begin = str.indexOf("{");
                    int end = str.lastIndexOf(")");
                    String jsonString = str.substring(begin, end);
                    //json解析
                    Gson gson = new Gson();
                    Log.i("TAG", "神一样的---------: " + jsonString);
                    GlobalChildBean globalChildBean = gson.fromJson(jsonString, GlobalChildBean.class);
                    Log.i(TAG, "能走不---: ");
                    mList = globalChildBean.getCommentShowDisplays();
                    initData();
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    t.printStackTrace();
                }
            });
        }


    /**
     * 初始化recyclerview数据
     */
    private void initData() {
        GlobalRecylerAdapter adapter = new GlobalRecylerAdapter(mContext, mList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager
                .VERTICAL, false);
        RV_Globalchild.setLayoutManager(layoutManager);
        RV_Globalchild.setItemAnimator(new DefaultItemAnimator());
        RV_Globalchild.addItemDecoration(new SpacesItemDecoration(10));
        RV_Globalchild.setAdapter(adapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }


}
